/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*/
package l1j.taki;

import java.util.ArrayList;

import l1j.server.server.ActionCodes;
import l1j.server.server.GeneralThreadPool;
import l1j.server.server.model.L1Inventory;
import l1j.server.server.model.L1Teleport;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1MonsterInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_BlueMessage;
import l1j.server.server.serverpackets.S_SystemMessage;

public class GirtasCave {

	public final ArrayList<L1PcInstance> _members = new ArrayList<L1PcInstance>();

	private static GirtasCave _instance;

	public static GirtasCave getInstance() {
		if (_instance == null) {
			_instance = new GirtasCave();
		}
		return _instance;
	}

	private static final int _readytime = 30 * 1000; //等待時間
	private static final int _cleartime = 10 * 1000; //清潔時間
	private static final int _speaktime = 10 * 1000;
	private static final int _waittime = 15 * 60 * 1000;
	private static int[] bossDeathCount = new int[6];
	
	public void StartGirtas(int mapid) {
		bossDeathCount[mapid-7200] ++;
			 if (bossDeathCount[mapid-7200]<10){
			sendMessage(mapid,"【新冥王】丹特斯：很好，黑法師已被擊殺！剩餘"+ (10 -bossDeathCount[mapid-7200]));
			} else {
				endGirtasCave(mapid);
			}

	}

	public String enterGirtasCave(L1PcInstance pc, L1ItemInstance item) throws Exception {
		L1PcInstance leader = pc.getParty().getLeader();
		L1PcInstance member[] = pc.getParty().getMembers();
		short mapId = 7200;
		int locX = 32648;
		int locY = 32921;
		mapId = (short) (7200 + getMembersCount());
		
		if (getMembersCount() >= 6) {
			pc.sendPackets(new S_SystemMessage("副本已滿。"));
			return "";
		}
		for (int i = 0, a = member.length; i < a; i++) {
			if (member[i].getId() == leader.getId() || member[i] == null)
				continue;
			L1Teleport.teleport(leader, locX, locY, mapId, pc.getHeading(), true);
			pc.setDailyMapID(mapId);
			pc.save();
			addMembers(pc);
			pc.getInventory().removeItem(item, 1);
			switch (member.length){
			case 8:
				if (member[7].getDailyCave() >= 1){
					L1Teleport.teleport(member[7], locX, locY, mapId, pc.getHeading(), true);
					member[7].setDailyMapID(mapId);
					member[7].save();
				} else {
					pc.sendPackets(new S_SystemMessage("隊員"+member[7].getName()+"今天還沒完成炎魔副本/巨龍副本，不能進入吉爾塔斯副本。"));
					member[7].sendPackets(new S_SystemMessage("你今天還沒完成炎魔副本/巨龍副本，不能進入吉爾塔斯副本。。"));
				}
			case 7:
				if (member[6].getDailyCave() >= 1){
					L1Teleport.teleport(member[6], locX, locY, mapId, pc.getHeading(), true);
					member[6].setDailyMapID(mapId);
					member[6].save();
				} else {
					pc.sendPackets(new S_SystemMessage("隊員"+member[6].getName()+"今天還沒完成炎魔副本/巨龍副本，不能進入吉爾塔斯副本。"));
					member[6].sendPackets(new S_SystemMessage("你今天還沒完成炎魔副本/巨龍副本，不能進入吉爾塔斯副本。。"));
				}
			case 6:
				member[5].save();
				if (member[5].getDailyCave() >= 1){
					L1Teleport.teleport(member[5], locX, locY, mapId, pc.getHeading(), true);
					member[5].setDailyMapID(mapId);
					member[5].save();
				} else {
					pc.sendPackets(new S_SystemMessage("隊員"+member[5].getName()+"今天還沒完成炎魔副本/巨龍副本，不能進入吉爾塔斯副本。"));
					member[5].sendPackets(new S_SystemMessage("你今天還沒完成炎魔副本/巨龍副本，不能進入吉爾塔斯副本。。"));
				}
			case 5:
				if (member[4].getDailyCave() >= 1){
					L1Teleport.teleport(member[4], locX, locY, mapId, pc.getHeading(), true);
					member[4].setDailyMapID(mapId);
					member[4].save();
				} else {
					pc.sendPackets(new S_SystemMessage("隊員"+member[4].getName()+"今天還沒完成炎魔副本/巨龍副本，不能進入吉爾塔斯副本。"));
					member[4].sendPackets(new S_SystemMessage("你今天還沒完成炎魔副本/巨龍副本，不能進入吉爾塔斯副本。。"));
				}
			case 4:				
				if (member[3].getDailyCave() >= 1){
					L1Teleport.teleport(member[3], locX, locY, mapId, pc.getHeading(), true);
					member[3].setDailyMapID(mapId);
					member[3].save();
				} else {
					pc.sendPackets(new S_SystemMessage("隊員"+member[3].getName()+"今天還沒完成炎魔副本/巨龍副本，不能進入吉爾塔斯副本。"));
					member[3].sendPackets(new S_SystemMessage("你今天還沒完成炎魔副本/巨龍副本，不能進入吉爾塔斯副本。。"));
				}
			case 3:
				if (member[2].getDailyCave() >= 1){
					L1Teleport.teleport(member[2], locX, locY, mapId, pc.getHeading(), true);
					member[2].setDailyMapID(mapId);
					member[2].save();
				} else {
					pc.sendPackets(new S_SystemMessage("隊員"+member[2].getName()+"今天還沒完成炎魔副本/巨龍副本，不能進入吉爾塔斯副本。"));
					member[2].sendPackets(new S_SystemMessage("你今天還沒完成炎魔副本/巨龍副本，不能進入吉爾塔斯副本。。"));
				}
			case 2:
				if (member[1].getDailyCave() >= 1){
					L1Teleport.teleport(member[1], locX, locY, mapId, pc.getHeading(), true);
					member[1].setDailyMapID(mapId);
					member[1].save();
				} else {
					pc.sendPackets(new S_SystemMessage("隊員"+member[1].getName()+"今天還沒完成炎魔副本/巨龍副本，不能進入吉爾塔斯副本。"));
					member[1].sendPackets(new S_SystemMessage("你今天還沒完成炎魔副本/巨龍副本，不能進入吉爾塔斯副本。。"));
				}
				break;
			}
			return "";
		}
		return "";
	}
	
	private void addMembers(L1PcInstance pc) {
		_members.add(pc); 
		GeneralThreadPool.getInstance().execute(new runTakiCaveDragon());
	}

	private class runTakiCaveDragon implements Runnable {
	
		public void run() {
	
			try {
				int mapid = 7199 + getMembersCount();
				bossDeathCount[mapid-7200] = 0;
				
				sendMessage(mapid,"【新冥王】丹特斯：可惡! ”黑法師部隊”竟然叛變了．．． ");
				Thread.sleep(_speaktime);
				
				sendMessage(mapid,"【新冥王】丹特斯：我的領軍中是不容許叛變者的！");
				Thread.sleep(_speaktime);
				
				sendMessage(mapid,"【新冥王】丹特斯：我給你們15分鐘，擊殺5只”叛變黑法師”");
				Thread.sleep(_speaktime);
				
				sendMessage(mapid,"【新冥王】丹特斯：時間內，如果沒有擊殺到任何”黑法師”結界將會關閉，請盡速起行！");
				Thread.sleep(_speaktime);
				
				sendMessage(mapid,"【新冥王】丹特斯：如果你們擊殺了足夠的叛徒，我將會給予你們相應獎勵”");
				Thread.sleep(10000);
				
				if (checkParty(mapid)){
					sendMessage(mapid,"【新冥王】丹特斯：注意，它們的黑魔法會變成附近的生物，很難從肉眼分別出來！");
					Thread.sleep(_speaktime);
					
					sendMessage(mapid,"【新冥王】丹特斯：不要想，不要管，殺光它們！");
					Thread.sleep(_waittime);
			
					sendMessage(mapid,"副本將於30秒後結束");
					Thread.sleep(_readytime);
					
					sendMessage(mapid,"倒數10秒");
					Thread.sleep(10*1000);
			
					sendMessage(mapid,"5秒");
					Thread.sleep(1000);
			
					sendMessage(mapid,"4秒");
					Thread.sleep(1000);
			
					sendMessage(mapid,"3秒");
					Thread.sleep(1000);
						
					sendMessage(mapid,"2秒");
					Thread.sleep(1000);
			
					sendMessage(mapid,"1秒");
					Thread.sleep(1000);
						
					endGirtasCave(mapid);
				}
				Thread.sleep(_cleartime);
				
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
	
		}
	}

	private boolean checkParty(int mapid) {
		for (L1PcInstance pc : L1World.getInstance().getAllPlayers()) {
			if (!pc.isInParty()){
				if (pc.getMapId() == mapid && pc.getDailyMapID() == mapid){
					sendMessage(mapid,"檢察到玩家沒有組隊，副本強制結束");
					L1Teleport.teleport(pc, 33442, 32797, (short) 4, pc.getHeading(), true);
					endGirtasCave(mapid);
					return false;
				}
			}
		}
		return true;
	}
	
	private void endGirtasCave(int mapid) {
		switch (mapid){
		case 7205:
			clearMembers();
		default:
			sendMessage(mapid,"副本結束");
			for (L1PcInstance pc : L1World.getInstance().getAllPlayers()) {
				if (pc.getMapId() == mapid){
						L1Teleport.teleport(pc, 33442, 32797, (short) 4, pc.getHeading(), true);
						pc.setDailyMapID(0);
						if (bossDeathCount[mapid-7200] >= 10){
							if ((pc.getLevel() >= 65 || pc.getLevelmet() > 0)){
								pc.getInventory().storeItem(70035, 1);
								pc.getInventory().storeItem(70041, 3);
								pc.sendPackets(new S_SystemMessage("獲得【副本徽章】3個。"));
								pc.sendPackets(new S_SystemMessage("獲得【吉爾塔斯寶箱】。"));
							} else {
								pc.sendPackets(new S_SystemMessage("等級未滿95，未能取得副本獎勵"));
							}
							pc.setDailyCave(3);
						}
						pc.saveInventory();
					}
				}
			clearColosseum(mapid);
			break;
		}
	}	
	
	private void clearColosseum(int mapid) {
		for (Object obj : L1World.getInstance().getVisibleObjects(mapid).values()) {
			if (obj instanceof L1MonsterInstance) {
				L1MonsterInstance mob = (L1MonsterInstance) obj;
				if (!mob.isDead()) {
					mob.setDead(true);
					mob.setStatus(ActionCodes.ACTION_Die);
					mob.setCurrentHpDirect(0);
					mob.deleteMe();
				}
			} else if (obj instanceof L1Inventory) {
				L1Inventory inventory = (L1Inventory) obj;
				inventory.clearItems();
			}
		}
	}
	
	private void sendMessage(int mapid, String msg) {
		for (L1PcInstance pc : L1World.getInstance().getAllPlayers()) {
			if (pc.getMapId() == mapid){
				pc.sendPackets(new S_BlueMessage(166,"\\f3" + msg));
			}
		}
	}	
	
	private void clearMembers() {
		_members.clear();
	}
	
	public int getMembersCount() {
		return _members.size();
	}

}